// import NProgress from "nprogress";

const perfix = "http://47.98.235.98:3000/api";
const prefix = "http://www.pudge.wang:4000";
const http = {
  get(url, params) {
    if (params) {
      url += "?";
      let arr = [];
      for (const key in params) {
        arr.push(key + "=" + params[key]);
      }
      url += arr.join("&");
      url.substring(0, url.length - 1);
    }

    // return new Promise(resolve => {
    return fetch(perfix + url)
      .then((response) => response.json())
      .then((res) => {
        return res;
      });
    // })

    // return fetch(url)
    //   .then(response => response)
    //   .then(res =>{
    //     resolve(res)
    //   })
  },
  post(url, data) {
    return new Promise((resolve) => {
      // NProgress.start();
      return fetch(perfix + url, {
        method: "POST", // or 'PUT'
        body: JSON.stringify(data), // data can be `string` or {object}!
        headers: new Headers({
          "Content-Type": "application/json",
        }),
      })
        .then((response) => response.json())
        .then((res) => {
          // NProgress.done();
          resolve(res);
        });
    });
  },

  post1(url, data) {
    return new Promise((resolve) => {
      // NProgress.start();
      return fetch(prefix + url, {
        method: "POST", // or 'PUT'
        body: JSON.stringify(data), // data can be `string` or {object}!
        headers: new Headers({
          "Content-Type": "application/json",
        }),
      })
        .then((response) => response.json())
        .then((res) => {
          // NProgress.done();
          resolve(res);
        });
    });
  },
};
export default http;
